/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao


import entity.PhoneNumber;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author JAVIER
 */
public class PhoneNumbersDAO implements DAO <PhoneNumber> {
    
    private Integer ID = 0;  
        
    private static final String SQL_INSERT = " INSERT INTO PhoneNumbers ( clientid, phoneNumber, type) VALUES ( ?, ?, ?)";
    
    private static final String SQL_SELECT = "SELECT * FROM PhoneNumbers where id= ?";
    
    private static final String SQL_SELECT_PROPERTY = " SELECT * FROM PhoneNumbers where ? = ?" ;
    
    private static final String SQL_SELECT_All = " SELECT * FROM PhoneNumbers";
    
    private static final String SQL_UPDATE = " UPDATE PhoneNumbers SET id=?, clientid=?, phoneNumber=?, type=? WHERE id = ?";

    private static final String SQL_DELETE = " DELETE FROM PhoneNumbers WHERE id = ? ";
    
    Integer create(PhoneNumber Phon, Connection connect) throws SQLException {

        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_INSERT, Statement.RETURN_GENERATED_KEYS);
      
            ps.setint(1, Phon.getClient().getId());
            ps.setString(2, Phon.getPhoneNumber());
            ps.setString(3,Phon.getType());

            ID = ps.executeUpdate();
        } finally {
            close(ps);
        }
        
        return ID;
        //TODO implement
    }
    
    Boolean remove(PhoneNumber Phon, Connection connect) throws SQLException {
        Boolean result = false;
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_DELETE);
            ps.setInt(1, Phon.getId());
            ps.executeUpdate();
            result = true;
        } finally {
            close(ps);
        }
        return result;
        //TODO implement
    }
    
    Boolean update(PhoneNumber Phon, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        try {
            ps = connect.prepareStatement(SQL_UPDATE, Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, Phon.getId());
            ps.setInt(2, Phon.getClient().getId());
            ps.setString(3, Phon.getPhoneNumber());
            ps.setString(4,Phon.getType());
            ps.setInt(5, Phon.getId());
            ID = ps.executeUpdate();
      
        } finally {
            close(ps);
        }
        return ID;
        //TODO implement
    }
    
    PhoneNumber find(Integer id, Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT);
            ps.setString(1, id);
            rs = ps.executeQuery();
            PhoneNumber phone = new PhoneNumber();
            result = null;
            phone = get_PhoneNumber(rs);
            return phone;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List findAll(Connection connect) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_All);
            rs = ps.executeQuery();
            List results = new ArrayList();
            PhoneNumber phone = new PhoneNumber();
            result = null;
            while (rs.next()) {
                phone = get_PhoneNumber(rs);
                results.add(phone);}
            return result;
        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
    
    List select_by_property(String camp, String Value, Connection connect)throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connect.prepareStatement(SQL_SELECT_PROPERTY);
            ps.setString(1, camp);
            ps.setString(2, value);
            rs = ps.executeQuery();
            List results = new ArrayList();
            PhoneNumber phone = new PhoneNumber();
            result = null;
            while (rs.next()) {
                phone = get_PhoneNumber(rs);
                results.add(phone);}
            return result;

        } finally {
            close(rs);
            close(ps);  
        }
        //TODO implement
    }
        
    PhoneNumber get_PhoneNumber (ResultSet rs) throws SQLException {
 
        PhoneNumber phone = new PhoneNumber();
        phone.setId(rs.getInt("id"));
        phone.setClientId(rs.getInt("clientId"));
        phone.setPhoneNumber(rs.getString("phoneNumber"));
        phone.setType(rs.getString("type"));

        return phone;
    }
    
}

